package com.vdg.fps.notifycation;

import java.util.HashMap;
import java.util.Map;

import com.vdg.fps.config.FPSConfig;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Vibrator;
import android.util.Log;

// Code to make a Service stay in the foreground from
// http://devtcg.blogspot.com/2009/12/gracefully-supporting-multiple-android.html
public class ServiceNotification {

	private NotificationManager mNotificationMGR;
	private Notification mNotification;
	private Vibrator mVibrator;
	private Intent mNotificationIntent;
	protected WakeLock mWakeLock;
	private Map<String, Integer> notificationCount = new HashMap<String, Integer>(
			2);
	private Map<String, Integer> notificationId = new HashMap<String, Integer>(
			2);
	protected static int SERVICE_NOTIFICATION = 1;
	private int lastNotificationId = 2;
	private Context mContext;
	private static final String APP_NAME = "S2Talk";
	private static ServiceNotification sInstance;

	public ServiceNotification(Context context) {
		mContext = context;
		mVibrator = (Vibrator) mContext
				.getSystemService(Context.VIBRATOR_SERVICE);
		mWakeLock = ((PowerManager) mContext
				.getSystemService(Context.POWER_SERVICE)).newWakeLock(
				PowerManager.PARTIAL_WAKE_LOCK, APP_NAME);
	}

	public static ServiceNotification getInstance(Context context) {
		if (sInstance == null) {
			sInstance = new ServiceNotification(context);
		}
		return sInstance;
	}

	public void showNotification(Service context, int id, Notification n) {
		Log.d("EclairAndBeyond", "showNotification " + id + " " + n);
		context.startForeground(id, n);
	}

	public void hideNotification(Service context, int id) {
		Log.d("EclairAndBeyond", "hideNotification");
		context.stopForeground(true);
	}

	private void setLEDNotification() {
		if (FPSConfig.IS_LED_NOTIFY) {
			mNotification.ledARGB = Color.MAGENTA;
			mNotification.ledOnMS = 300;
			mNotification.ledOffMS = 1000;
			mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
		}
	}

	public void resetNotificationCounter(String userJid) {
		notificationCount.remove(userJid);
	}

	public void clearNotification(String Jid) {
		int notifyId = 0;
		if (notificationId.containsKey(Jid)) {
			notifyId = notificationId.get(Jid);
			mNotificationMGR.cancel(notifyId);
		}
	}
}
